<?php

/**
 * Class User of GXC CMS, extends from CWebUser
 * 
 * 
 * @author Tuan Nguyen  <nganhtuan63@gmail.com>
 * @version 1.0
 * @package cms.components.user
 */
class LGUser extends CWebUser {

    public $superAdmin = null;
    /**
     * Get the Model from the session of Current User
     * @return Object from Session of Current User
     */
    public function getModel($attr = '') {
        $user = Yii::app()->getSession()->get('current_user');
        if ($attr == '')
            return $user;
        else {
            return $user[$attr];
        }
    }

    /** This is a function that checks the field 'role'
     * in the User model to be equal to 1, that means it's admin
     * 
     * access it by Yii::app()->user->isAdmin()
     */
    public function checkSuperAdmin() {
        if (user()->name === user()->superAdmin) {
            return true;
        }
        return false;
    }

    /**
     * Function to check from Before Login if it is from Cookie
     * @param type $id
     * @param type $states
     * @param type $fromCookie
     * @return type 
     */
    public function beforeLogin($id, $states, $fromCookie) {
        return true;
    }

    /**
     * Login Function
     *  
     * @param UserIndentity $identity
     * @param int $duration 
     */
    public function login($identity, $duration = 3600) {
        return parent::login($identity, $duration);
    }

    /**
     * Logout Function
     * @param boolean $destroySession destroy the session or not
     */
    public function logout($destroySession = true) {
        // I always remove the session variable model.
        Yii::app()->getSession()->remove('current_user');
        Yii::app()->session->clear();
        Yii::app()->session->destroy();

        parent::logout();
    }

    /**
     * Actions to be taken after logging in.
     * Overloads the parent method in order to mark superusers.
     * @param boolean $fromCookie whether the login is based on cookie.
     */
    public function afterLogin($fromCookie) {
        parent::afterLogin($fromCookie);

        //add code after login
        //TO DO
    }

    public function checkAccess($operation, $params = array(), $allowCaching = true) {
        return  $this->checkSuperAdmin()===true ? true : parent::checkAccess($operation, $params, $allowCaching);        
    }


}

?>
